package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/5.
 */
@Component
public interface ShopManageDao {
    /**
     * 查询所有的商家
     * @return
     */
    @Select("<script>select * from seller_shopinfo where shop_type=1 and audit_status=1" +
            "<if test=\"id!=null and id!=''\">and id=#{id}</if>" +
            "<if test=\"shopName!=null and shopName!=''\"> and shop_name like concat('%',#{shopName},'%') </if>" +
            "</script>")
    List<Map<String,Object>>queryAllSeller(Pagination page,@Param("id")Integer id, @Param("shopName")String shopName);

    /**
     * 修改对应商家的信息
     * @param shopInfo
     * @return
     */
    @Update("update seller_shopinfo set " +
            "shop_name=#{shopInfo.shop_name}, " +
            "shop_title=#{shopInfo.shop_title}, " +
            "shop_address=#{shopInfo.shop_address}, " +
            "seller_email=#{shopInfo.seller_email}, " +
            "mobile=#{shopInfo.mobile}, " +
            "notice=#{shopInfo.notice}" +
            "where id=#{shopInfo.id}")
    int updateSeller(@Param("shopInfo")Map<String,Object>shopInfo);

    /**
     * 查询店铺所拥有的货物
     * @param id
     * @return
     */
    @Select("select g.*,s.shop_price seller_price, " +
            "DATE_FORMAT(promote_start_date,'%Y-%m-%d %T')promote_start_dates, " +
            "DATE_FORMAT(promote_end_date,'%Y-%m-%d %T')promote_end_dates, " +
            "DATE_FORMAT(add_time,'%Y-%m-%d %T')add_times, " +
            "DATE_FORMAT(xiangou_start_date,'%Y-%m-%d %T')xiangou_start_dates, " +
            "DATE_FORMAT(xiangou_end_date,'%Y-%m-%d %T')xiangou_end_dates " +
            "from goods g left join user_seller_goods s on s.id=#{id} where g.goods_id=s.goods_id")
    List<Map<String,Object>>queryShopGoodById(Pagination page,@Param("id")Integer id);

    /**
     * 查询店铺所拥有的货物
     *@author xxf
     * @date 2018/2/23
     * @Param id
     * @return
     */
    @Select("<script>" +
            "SELECT s.shop_price seller_price,s.cost_price scost_price,t.goods_id, t.parent_goods_id,t.cat_id,t.user_id," +
            "(select user_name from INFS_ADMIN_USER where user_id = t.user_id) user_name," +
            "t.goods_sn,t.goods_name,t.click_count,t.brand_id," +
            "t.goods_number,t.goods_weight,t.market_price,t.cost_price,t.supply_price,t.shop_price,t.promote_price," +
            "DATE_FORMAT(t.promote_start_date,'%Y-%m-%d %T')promote_start_date," +
            "DATE_FORMAT(t.promote_end_date,'%Y-%m-%d %T')promote_end_date," +
            "t.warn_number,t.keywords,t.goods_brief,t.goods_desc,t.goods_thumb,t.goods_img,t.is_real,t.is_on_sale," +
            "t.is_alone_sale,t.add_time,t.sort_order,t.is_delete,t.is_best,t.is_new,t.is_hot,t.is_promote,t.goods_type,t.seller_note," +
            "t.give_integral,t.rank_integral,t.is_xiangou,t.mod_count, " +
            "DATE_FORMAT(t.xiangou_start_date,'%Y-%m-%d %T')xiangou_start_date," +
            "DATE_FORMAT(t.xiangou_end_date,'%Y-%m-%d %T')xiangou_end_date," +
            "t.xiangou_num,t.goods_tag,t.measure_unit,t.from_seller_id," +
            "(SELECT shop_name from seller_shopinfo where id = t.from_seller_id) from_seller_name," +
            "<![CDATA[(case when t.is_promote=1 and NOW() BETWEEN t.promote_start_date AND t.promote_end_date then 1 else 0 end)]]> price_type," +
            "b.brand_name,c.cat_name,g.cat_name typeName ,DATE_FORMAT(t.add_time,'%Y-%m-%d %T') add_times," +
            "(SELECT sum(og.goods_number) FROM order_goods og WHERE og.goods_id = t.goods_id and exists(select * from order_info oi where oi.order_id = og.order_id and oi.order_status=1)) good_sales," +
            "(SELECT iu.user_type FROM INFS_USER iu WHERE iu.user_id=#{userId}) user_type," +
            "(SELECT sum(og.goods_number) FROM order_goods og WHERE og.goods_id = t.goods_id and exists(select * from order_info oi where oi.order_id = og.order_id and oi.order_status=1" +
            " and exists(select * from infu_distributor_member dm where dm.user_id = oi.user_id and dm.d_user_id =#{userId}))) dis_sales" +
            " FROM goods t " +
            " LEFT JOIN brand b on t.brand_id =b.brand_id " +
            " LEFT JOIN category c on c.cat_id=t.cat_id " +
            " LEFT JOIN goods_type g on t.goods_type=g.cat_id " +
            " LEFT JOIN user_seller_goods s on s.id=#{theShopId} " +
            "<where>" +
            "t.goods_id=s.goods_id "+
            "<if test=\"goods_id!=null and goods_id!=''\">t.goods_id=#{goods_id}</if>" +
            "<if test=\"goods_name!=null and goods_name!=''\"> and t.goods_name=#{goods_name}</if>" +
            "<if test=\"cat_id!=null and cat_id!=''\"> and t.cat_id=#{cat_id}</if>" +
            "<if test=\"brand_id!=null and brand_id!=''\"> and t.brand_id=#{brand_id}</if>" +
            "</where>" +
            "</script>")
    List<Map<String,Object>> queryShopGoodByShopId(Pagination page, Map<String,Object> param);

    /**
     * 查询所有的货物
     * @param page
     * @return
     */
    @Select("select g.*," +
            "DATE_FORMAT(promote_start_date,'%Y-%m-%d %T')promote_start_dates, " +
            "DATE_FORMAT(promote_end_date,'%Y-%m-%d %T')promote_end_dates, " +
            "DATE_FORMAT(add_time,'%Y-%m-%d %T')add_times, " +
            "DATE_FORMAT(xiangou_start_date,'%Y-%m-%d %T')xiangou_start_dates, " +
            "DATE_FORMAT(xiangou_end_date,'%Y-%m-%d %T')xiangou_end_dates " +
            " from goods g where goods_id not in(select goods_id from user_seller_goods where id=#{id})")
    List<Map<String,Object>>queryAllGoods(Pagination page,@Param("id")Integer id);

    /*
    * 查询所有的货物
    * */
    @Select("<script>" +
            "SELECT t.goods_id, t.parent_goods_id,t.cat_id,t.user_id," +
            "(select user_name from INFS_ADMIN_USER where user_id = t.user_id) user_name," +
            "t.goods_sn,t.goods_name,t.click_count,t.brand_id," +
            "t.goods_number,t.goods_weight,t.market_price,t.cost_price,t.supply_price,t.shop_price,t.promote_price," +
            "DATE_FORMAT(t.promote_start_date,'%Y-%m-%d %T')promote_start_date," +
            "DATE_FORMAT(t.promote_end_date,'%Y-%m-%d %T')promote_end_date," +
            "t.warn_number,t.keywords,t.goods_brief,t.goods_desc,t.goods_thumb,t.goods_img,t.is_real,t.is_on_sale," +
            "t.is_alone_sale,t.add_time,t.sort_order,t.is_delete,t.is_best,t.is_new,t.is_hot,t.is_promote,t.goods_type,t.seller_note," +
            "t.give_integral,t.rank_integral,t.is_xiangou,t.mod_count, " +
            "DATE_FORMAT(t.xiangou_start_date,'%Y-%m-%d %T')xiangou_start_date," +
            "DATE_FORMAT(t.xiangou_end_date,'%Y-%m-%d %T')xiangou_end_date," +
            "t.xiangou_num,t.goods_tag,t.measure_unit,t.from_seller_id," +
            "(SELECT shop_name from seller_shopinfo where id = t.from_seller_id) from_seller_name," +
            "<![CDATA[(case when t.is_promote=1 and NOW() BETWEEN t.promote_start_date AND t.promote_end_date then 1 else 0 end)]]> price_type," +
            "b.brand_name,c.cat_name,g.cat_name typeName ,DATE_FORMAT(t.add_time,'%Y-%m-%d %T') add_times," +
            "(SELECT sum(og.goods_number) FROM order_goods og WHERE og.goods_id = t.goods_id and exists(select * from order_info oi where oi.order_id = og.order_id and oi.order_status=1)) good_sales," +
            "(SELECT iu.user_type FROM INFS_USER iu WHERE iu.user_id=#{userId}) user_type," +
            "(SELECT sum(og.goods_number) FROM order_goods og WHERE og.goods_id = t.goods_id and exists(select * from order_info oi where oi.order_id = og.order_id and oi.order_status=1" +
            " and exists(select * from infu_distributor_member dm where dm.user_id = oi.user_id and dm.d_user_id =#{userId}))) dis_sales" +
            " FROM goods t " +
            " LEFT JOIN brand b on t.brand_id =b.brand_id " +
            " LEFT JOIN category c on c.cat_id=t.cat_id " +
            " LEFT JOIN goods_type g on t.goods_type=g.cat_id " +
            "<where>" +
            "goods_id not in(select goods_id from user_seller_goods where id=#{theShopId})"+
            "<if test=\"goods_id!=null and goods_id!=''\">t.goods_id=#{goods_id}</if>" +
            "<if test=\"goods_name!=null and goods_name!=''\"> and t.goods_name=#{goods_name}</if>" +
            "<if test=\"cat_id!=null and cat_id!=''\"> and t.cat_id=#{cat_id}</if>" +
            "<if test=\"brand_id!=null and brand_id!=''\"> and t.brand_id=#{brand_id}</if>" +
            "</where>" +
            "</script>")
    List<Map<String,Object>> queryGoods(Pagination page, Map<String,Object> param);

    /**
     * 插入对应商家的货物
     * @param goodsInfo
     * @return
     */
    @Insert("insert into user_seller_goods (goods_id,id,shop_price,goods_name,goods_sn,cost_price)values(#{GoodsInfo.goods_id},#{GoodsInfo.id},#{GoodsInfo.shop_price},#{GoodsInfo.goods_name},#{GoodsInfo.goods_sn},#{GoodsInfo.supply_price})")
    int insertShopGoods(@Param("GoodsInfo")Map<String,Object>goodsInfo);

    /**
     * 删除对应商店的商品
     * @param id
     * @param goods_id
     * @return
     */
    @Delete("delete from user_seller_goods where id=#{id} and goods_id=#{goodsId}")
    int deleteShopGoods(@Param("id")Integer id,@Param("goodsId")Integer goods_id);
}
